$(function () {
    //定义一个全局变量
    //将来筛选和分页都可以修改
    let query = {
        pagenum: 1,
        pagesize: 3,
        cate_id: '',
        state: '',
    }
    //需求1:展示文章列表
    let layer = layui.layer
    initArticleList()
    //封装成函数,方便调用
    function initArticleList() {
        axios({
            methoid: 'GET',
            url: '/my/article/list',
            params: query
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            let arr = []
            res.data.data.forEach(ele => {
                //console.log(ele)
                arr.push(`
                <tr>
                   <td>${ele.title}</td>
                   <td>${ele.cate_name}</td>
                   <td>${dateFormat(ele.pub_date)}</td>
                   <td>${ele.state}</td>
                   <td>
                     <a href="/article/art_edit.html?id=${ele.Id}" class="layui-btn layui-btn-xs">
                       编辑
                     </a>
                     <button
                       data-id='${ele.Id}'
                       
                       class="btn-delete layui-btn layui-btn-xs layui-btn-danger"
                     >
                       删除
                     </button>
                 </td>
              </tr>
                `)
            })
            //赋值
            $("tbody").empty().html(arr.join(''))
            //赋值之后做分页;
            pageRender(res.data.total)
        })
    }
    //需求2: 初始化文章分类


    let form = layui.form;
    initArtCateList();
    function initArtCateList() {
        axios({
            method: "GET",
            url: "/my/article/cates",
        }).then((res) => {
            //判断
            //console.log(res.data);
            if (res.data.status) {
                return layer.msg(res.data.message);
            }
            //成功获取后,赋值渲染
            let arr = [`<option value="">请选择文章类别</option>`];
            res.data.data.forEach(function (ele) {
                arr.push(`
                <option value="${ele.Id}">${ele.name}</option>
                `);
            });
            //赋值
            let str = arr.join("");
            $("[name=cate_id]").empty().html(str);
            //form表单同步渲染
            form.render();
        });
    }
    //需求3:筛选
    $("#formSearch").on('submit', function (e) {

        e.preventDefault()
        query.cate_id = $("[name=cate_id]").val()
        query.state = $("[name=state]").val()
        initArticleList()
    })
    //需求4:分页
    var laypage = layui.laypage;
    function pageRender(total) {
        //执行一个laypage实例
        laypage.render({
            elem: 'pageBox',
            count: total,
            limit: query.pagesize,
            curr: query.pagenum,
            //配置定制化的分页
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    query.pagenum = obj.curr;
                    query.pagesize = obj.limit;
                    initArticleList()
                }
            }
        });
    }

    //需求5: 删除
    $("tbody").on('click', '.btn-delete', function () {
        let id = $(this).attr('data-id')

        //弹出层删除
        layer.confirm('确认删除吗?', { icon: 3, title: '提示' }, function (index) {
            //do something
            axios({
                method: '',
                url: "/my/article/delete/" + id,
            }).then(res => {
                if (res.data.status != 0) {
                    return layer.msg(res.data.message)
                }
                layer.msg(res.data.message)

                if ($('.btn-delete').length == 1 && query.pagenum >= 2) {
                    query.pagenum--
                }
                initArticleList()
            })

            layer.close(index);
        });
    })

})